package com.tanhua.server.controller;

import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class UndatePhoneController {

    @Autowired
    private UserService userService;

    /**
     * 修改手机号
     * 发送验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(@RequestHeader("Authorization") String token) {
        //获取手机号
        String phone = UserHolder.getMobile();
        //调用service发送验证码的方法
        userService.sendMsg(phone);
        return ResponseEntity.ok(null);
    }
    /**
     * 校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map, @RequestHeader("Authorization") String token){
        //获取手机号和验证码
        String phone = UserHolder.getMobile();
        String verificationCode = (String) map.get("verificationCode");
        boolean verification= userService.verification(phone, verificationCode);
        return ResponseEntity.ok(verification);


    }
    /**
     * 保存手机号
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map,@RequestHeader("Authorization") String token){
        //获取新手机号和当前登录的手机号
        String phone = (String) map.get("phone");
        String mobile = UserHolder.getMobile();
        //调用service
        userService.updatePhone(phone,mobile);
        return ResponseEntity.ok(null);

    }
}
